package net.mihome.app.ui;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;

import net.mihome.app.AppContext;
import net.mihome.app.R;
import net.mihome.app.bean.RssNews;
import net.mihome.app.bean.URLs;
import net.mihome.app.bean.net.ResponsePojo;
import net.mihome.app.common.UIHelper;
import net.mihome.app.utils.MD5;
import net.mihome.app.utils.MiAsyncHttpResponseHandler;

import org.apache.http.Header;

import java.util.Map;

public class RssWebViewActivity extends Activity {
	private WebView webView;
    private View backView;
	private Handler handler;
	private String html="没有数据";
	private String url=null;
	private AppContext appContext;
    private TextView pubDateView;
    private TextView titleView;
	private View progressView;
	public final static int START_PROGRESS=100;
	public final static int END_PROGRESS=200;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news_detail);
		appContext=(AppContext) getApplication();
        backView=findViewById(R.id.news_detail_home_bak);
        pubDateView= (TextView) findViewById(R.id.news_detail_date);
        titleView= (TextView) findViewById(R.id.news_detail_title);
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RssWebViewActivity.this.finish();
            }
        });
		webView=(WebView) findViewById(R.id.news_detail_webview);
		url=getIntent().getStringExtra("href");
        RequestParams requestParams=new RequestParams();
		progressView=findViewById(R.id.news_detail_head_progress);
        requestParams.add("href_md5", MD5.Md5(url).toUpperCase());
        AppContext.ASYNC_HTTP_CLIENT.get(this, URLs.MIHOME_GET_RSS_DETAIL,requestParams,new MiAsyncHttpResponseHandler<Map>() {
            @Override
            public void onSuccess(int arg0, Header[] arg1, ResponsePojo<Map> responsePojo) {
                RssNews rssNews=RssNews.parse(responsePojo.getData());
                pubDateView.setText(rssNews.getPubDates());
                titleView.setText(rssNews.getTitle().trim());
                webView.getSettings().setDefaultTextEncodingName("UTF -8");//设置默认为utf-8
                String mianzie="<br /><a href='"+url+"' >查看原文</a>";
                webView.loadData(rssNews.getText()+mianzie, "text/html; charset=UTF-8", null);//这种写法可以正确解码
                progressView.setVisibility(View.GONE);
            }

            @Override
            public void onFailure(int arg0, Header[] arg1, Throwable arg3) {
                UIHelper.showAlertDialog(RssWebViewActivity.this,"抱歉","加载失败!");
                progressView.setVisibility(View.GONE);
            }
        });
//		url=checkUrl(url);
//		progressView=findViewById(R.id.news_detail_head_progress);
//		handler=new Handler(new Handler.Callback() {
//			@Override
//			public boolean handleMessage(Message arg0) {
//				switch (arg0.what) {
//				case 1:
////					webView.loadDataWithBaseURL(url,html, "text/html","utf-8", null);
//					webView.loadUrl(url);
//					webView.setWebViewClient(UIHelper.getEnabelWebViewClient(new MihomeWebLinser() {
//						@Override
//						public void onReceivedError(WebView view, int errorCode,
//								String description, String failingUrl) {
//							// TODO Auto-generated method stub
//						}
//						@Override
//						public void onPageStarted(WebView view, String url, Bitmap favicon) {
////							handler.sendEmptyMessage(START_PROGRESS);
//							progressView.setVisibility(View.VISIBLE);
//						}
//						@Override
//						public void onPageFinished(WebView view, String url) {
//							// TODO Auto-generated method stub
//							handler.sendEmptyMessage(END_PROGRESS);
//							progressView.setVisibility(View.GONE);
//						}
//						@Override
//						public void onLoadResource(WebView view, String url) {
//							// TODO Auto-generated method stub
//							progressView.setVisibility(View.VISIBLE);
//						}
//					}));
//					handler.sendEmptyMessage(END_PROGRESS);
//					break;
//				case -1:
//					Toast.makeText(appContext,R.string.network_not_connected, Toast.LENGTH_SHORT).show();
//					handler.sendEmptyMessage(END_PROGRESS);
//					break;
//				default:
//					break;
//				}
//				return false;
//			}
//		});
//		handler.sendEmptyMessage(1);
	}
	/*
	 * 新浪rss提供的url是通过一个地址中专的
	 * http://go.rss.sina.com.cn/redirect.php?url=http://news.sina.com.cn/w/2014-10-16/214231000519.shtml
	 * 这个函数是做个处理
	 * **/
	public String checkUrl(String url){
		if(url!=null)
			return url=url+"?from=wap";
		else
			return url;
	}
}
